package com.example.video.fg

import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Toast
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.example.base.mvvm.BaseFragment
import com.example.video.R
import com.example.video.adapter.TypeAdapter
import com.example.video.adapter.VideoAdapter
import com.example.video.databinding.FragmentHomeBinding
import com.example.video.room.AppDBUtils
import com.example.video.room.StarEntity
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.youth.banner.loader.ImageLoader


class HomeFragment : BaseFragment<FragmentHomeBinding,HomeViewModel>() {
    val imgs = mutableListOf<String>()
    lateinit var typeAdapter:TypeAdapter
    lateinit var videoAdapter: VideoAdapter
    var channelId = "94349546935"//分类Id
    var page = 1//页码
    override fun getLayout(): Int  = R.layout.fragment_home

    override fun getViewModel(): Class<HomeViewModel>  = HomeViewModel::class.java

    override fun initView() {
        viewModel.bannerVideo()
        //新闻类型水平滑动
        typeAdapter = TypeAdapter()
        binding.rvType.adapter = typeAdapter
        binding.rvType.layoutManager = LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false)

        viewModel.typeVideo()

        videoAdapter = VideoAdapter()
        binding.rvVideo.adapter = videoAdapter
        binding.rvVideo.layoutManager = LinearLayoutManager(context)

        viewModel.video(channelId,page,10)

        //点击分类，视频数据发生变化
        typeAdapter.setOnItemClickListener { adapter, view, position ->
            channelId = typeAdapter.data[position].channelid
            page = 1//当切换分类时一定将页面重置为1
            typeAdapter.setId(channelId)
            viewModel.video(channelId,page,10)
        }
        //刷新加载更多
        binding.smart.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page=1
                viewModel.video(channelId,page,10)
                binding.smart.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                viewModel.video(channelId,page,10)
                binding.smart.finishLoadMore()
            }
        })
        //点击item上的某个控件
        videoAdapter.addChildClickViewIds(R.id.video_star,R.id.video_gz)
        videoAdapter.setOnItemChildClickListener { adapter, view, position ->
            if(view.id==R.id.video_star){
                star(position)
            }else if(view.id ==R.id.video_gz){

            }
        }

    }
    //收藏取消收藏
    private fun star(position:Int) {
        //查询数据库中是否存在
        val list = AppDBUtils.getDb().getStarDao().queryById(videoAdapter.data[position].id)
        //存在，证明已经收藏了，点击将执行取消收藏功能
        if(list.size>0){
            AppDBUtils.getDb().getStarDao().delete(list[0])
            (videoAdapter.getViewByPosition(position,R.id.video_star) as ImageView).setImageResource(R.drawable.ic_star)
            ToastUtils.showLong("取消收藏成功")
        }else{
            AppDBUtils.getDb().getStarDao().insert(StarEntity(videoAdapter.data[position].id,videoAdapter.data[position].title,videoAdapter.data[position].videomainimag))
            (videoAdapter.getViewByPosition(position,R.id.video_star) as ImageView).setImageResource(R.drawable.ic_star_yes)
            ToastUtils.showLong("收藏成功")
        }
    }

    override fun initData() {
        viewModel.bannerSuccess.observe(this){
            if(it.code==200){
                it.data.forEach {
                    imgs.add(it.imageUrl)
                }
                binding.banner.setImages(imgs)
                    .setImageLoader(object :ImageLoader(){
                        override fun displayImage(
                            context: Context?,
                            path: Any?,
                            imageView: ImageView?
                        ) {
                            if (context != null) {
                                if (imageView != null) {
                                    Glide.with(context).load(path).into(imageView)
                                }
                            }
                        }
                    })
                    .start()
            }
        }
        viewModel.typeSuccess.observe(this){
            if(it.code==200){
                typeAdapter.data.clear()
                typeAdapter.data.addAll(it.data)
                typeAdapter.notifyDataSetChanged()
            }
        }
        viewModel.videoSuccess.observe(this){
            if(it.code==200){
                if(page==1){
                    videoAdapter.data.clear()
                }
                videoAdapter.data.addAll(it.data)
                videoAdapter.notifyDataSetChanged()
            }
        }
    }

}